package club.haomoon.controller;

import club.haomoon.util.QRCodeGenerator;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.awt.*;

/**
 * -------------------------
 * @Description:
 * @Author: Wangjianwei
 * @Date: 2024/07/10 17:12
 * -------------------------
 */
@Slf4j
@Controller
public class QRCodeController {
    /**
     * qrcodeImage 原始zxing生成qrcode
     * @param orderNo
     * @return
     */
    @RequestMapping("/qrcode/image")
    public String qrcodeImage(String orderNo) {
        String failPath = "D:\\tmp\\" + orderNo + ".png";
        try {
            QRCodeGenerator.generateQRCodeImage(orderNo, 350, 350,  failPath);
        } catch (Exception e) {
            log.error("生成二维码失败", e);
        }
        return failPath;
    }

    /**
     * qrcodeBase64
     * @param orderNo
     * @return
     */
    @RequestMapping("/qrcode/base64")
    public String qrcodeBase64(String orderNo) {
        String message = "";
        try {
            message = QRCodeGenerator.writeToStream(orderNo, 350, 350);
        } catch (Exception e) {
            log.error("生成二维码失败", e);
        }
        return message;
    }


    /**
     * -------------------------
     * @Description:生成带有logo的二维码
     * @Author: Wangjianwei
     * @Date: 2024/07/10 17:19
     * @return java.lang.String
     * -------------------------
     */
    @RequestMapping("/qrcode/image1")
    public String hutoolsImages1() {
        String message = "";
        try {
            QrConfig config = new QrConfig(300, 300);
            // Set the margin, that is, the margin between the QR code and the background
            config.setMargin(3);
            // Set the foreground color, which is the QR code color (cyan)
            config.setForeColor(Color.CYAN.getRGB());
            // Set background color (gray)
            config.setBackColor(Color.GRAY.getRGB());
            // Generate QR code to file or stream
            QrCodeUtil.generate("http://www.liuhiahua.cn/", config, FileUtil.file("D:\\tmp\\hblog1.png"));
        } catch (Exception e) {
           log.error("生成二维码失败", e);
        }
        return message;
    }

    /**
     * -------------------------
     * @Description:采用hutool生成qrcode
     * @Author: Wangjianwei
     * @Date: 2024/07/10 17:20
     * @return java.lang.String
     * -------------------------
     */
    @RequestMapping("/qrcode/image2")
    public String hutoolsImages2() {
        String message = "";
        try {
            QrCodeUtil.generate(//
                    "http://www.liuhiahua.cn/", //content
                    QrConfig.create().setImg("D:\\tmp\\logo.png"), //logo
                    FileUtil.file("D:\\tmp\\qrcodeWithLogo.jpg")//output file
            );
        } catch (Exception e) {
            log.error("生成二维码失败", e);
        }
        return message;
    }
}
